package com.zhangyu.shadow.repair.delegate.irregular.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;
import com.zhangyu.shadow.repair.delegate.irregular.IrregularBusContract;
import com.zhangyu.shadow.repair.delegate.irregular.IrregularBusListAdapter;
import com.zhangyu.shadow.repair.delegate.irregular.IrregularBusPresenter;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;

import butterknife.BindView;
import st.lib.base.delegate.mvp.MvpDelegate;

/**
 * @author zhangyu
 * @date 2018/12/3
 */

public class SearchDelegate extends MvpDelegate<IrregularBusPresenter> implements
        IrregularBusContract.View {
    public static SearchDelegate create() {
        return new SearchDelegate();
    }

    @BindView(R2.id.toolbar_search)
    Toolbar mToolbar;
    @BindView(R2.id.et_search)
    EditText mEditTextSearch;
    @BindView(R2.id.rg_type)
    RadioGroup mRgType;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private int mType = 0;
    private IrregularBusListAdapter mBusListAdapter;

    @Override
    public void showLoading(String str) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void getVehicleListSuccess(ArrayList<VehicleBean> list) {
        mBusListAdapter.setNewData(list);
    }

    @Override
    public void getVehicleListFailed(int code, String msg) {

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_irregular_search;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int i = item.getItemId();
                if (i == R.id.menu_search) {
                    if (!mEditTextSearch.getText().toString().isEmpty()) {
                        search(mEditTextSearch.getText().toString());
                    }
                }
                return true;
            }
        });
        mToolbar.inflateMenu(R.menu.search);

        mEditTextSearch.setHint("请输入车辆编号");
        mEditTextSearch.setInputType(InputType.TYPE_CLASS_NUMBER);
        mEditTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (!mEditTextSearch.getText().toString().isEmpty()) {
                        search(mEditTextSearch.getText().toString());
                    }
                    return true;
                }
                return false;
            }
        });
        mRgType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_bus_put) {
                    //待交车
                    mType = 0;
                } else if (i == R.id.rb_bus_repair) {
                    //小修异常
                    mType = 1;
                } else {
                    mType = 2;
                }
            }
        });

        mBusListAdapter = new IrregularBusListAdapter(getContext(),
                R.layout.item_complete_vehicle_info, null);
        mBusListAdapter.setEmptyView(R.layout.view_list_empty, (ViewGroup) mRecyclerView.getParent());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mBusListAdapter);
    }

    private void search(String code) {
        mPresenter.getVehicleList(mType, code, AccountManager.getInstance()
                .getDepId(), "", 0);
    }
    @Override
    public boolean onBackPressedSupport() {
        if (!isFastClick()) {
            pop();
        }
        return true;
    }

    @Override
    public IrregularBusPresenter getPresenter() {
        return new IrregularBusPresenter(this);
    }
}
